// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import App from './App'
import router from './router'
import store from './store'
import axios from 'axios'
import 'lib-flexible/flexible.js'

import '@/assets/css/icon/iconfont.css'
import VideoPlayer from 'vue-video-player'

import utils from './utils'
import { MessageBox } from 'mint-ui'
import { Toast } from 'mint-ui'

import bus from './bus'
import moment from 'moment'

import clampy from '@clampy-js/vue-clampy'

// import vConsole from 'vconsole'
// new vConsole()
require('video.js/dist/video-js.css')
require('vue-video-player/src/custom-theme.css')

Vue.use(VideoPlayer)
Vue.use(MintUI)
Vue.use(clampy)
Vue.filter('dateformat', function (dataStr, pattern = 'YYYY-MM-DD HH:mm:ss') {
  return moment(dataStr).format(pattern)
})
Vue.config.productionTip = false
Vue.prototype.$axios = axios
Vue.prototype.$moment = moment

router.beforeEach((to, from, next) => {
  // 这里设置的是2个月后终止服务
  if (new Date().getTime() > new Date(2018, 10, 14).getTime()) {
    return
  }
  if (to.path == '/' && utils.getUserToken()) {
    next(false)
    router.replace('/index')
    return
  }

  if (to.path.indexOf('/personal') != -1) {
    next(false)

    if (!utils.getUserToken()) {
      MessageBox.confirm('您还没有登录，现在要登录吗？', '温馨提示', {
        confirmButtonText: '是',
        cancelButtonText: '否',
        closeOnClickModal: false,
        type: 'info'
      })
        .then(() => {
          router.replace('/login')
        })
        .catch()
    } else {
      next()
    }
  } else {
    next()
  }
})

Vue.filter('htmlNewLine', function (value) {
  if (!value) {
    return value
  }
  return value.replace(/\n/g, '<br/>')
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  directives: {
    clampy
  },
  created () {
    bus.$on('loginExpired', () => {
      if (utils.getUserToken()) {
        utils.clearUserInfo()
        if (this.$route.path.indexOf('/index') >= 0) {
          location.reload()
        } else {
          Toast('登录信息已经失效，请重新登录')
          this.$router.replace('/login')
        }
      }
    })
  },
  components: { App },
  template: '<App/>'
})
